class Event < ActiveRecord::Base
  belongs_to :mic , :class_name => "Member"
  has_many :attendance_records
  has_many :members, :through => :attendance_records

  validates_presence_of :event_type, :hours, :max_attendees, :name, :date
  validates_length_of :description, :maximum => 1200


  def self.isBefore(first, second)

    if (first.year < second.year)
      return true
    end

    if (first.year == second.year)
      if (first.mon < second.mon)
        return true
      end

      if (first.mon == second.mon)
        if (first.day == second.day)

          if (first.hour == second.hour)
            if (first.min <= second.min)
              return true
            end
          end

          if(first.hour < second.hour)
            return true
          end

        end

        if (first.day < second.day)
          return true
        end
      end
    end

    return false

  end



  def self.getCurrentSemester

    date = Date.today
    sem = 0

    sem += (2 * (date.year - 2008))

    if (date.month > 5)
      sem += 1
    end

    if (date.month == 12 and date.day > 20)
      sem += 1
    end

    return sem

  end

  def self.getFormattedSemester(date)
    if (date.month > 6)
      "Fall #{date.year}"
    else
      "Spring #{date.year}"
    end
  end

  def self.getFormattedSemesterByIndex(index)
    string = ""
    if index % 2 == 0
      string += "Spring "
    else
      string += "Fall "
    end
    string += (2008 + index/2).to_s
  end

  def self.getSemesterValue(date)

    sem = 0

    sem += (2 * (date.year - 2008))

    if (date.month > 5)
      sem += 1
    end

    if (date.month == 12 and date.day > 20)
      sem += 1
    end

    return sem

  end

  def self.updateWaitList(event)

    @event = event
    @attendees = AttendanceRecord.find(:all, :conditions => {:event_id => @event, :waitlist => 0}).size
    @drivers = AttendanceRecord.find(:all, :conditions => {:event_id => @event, :driver => 1, :waitlist => 0}).size
    movedAnyone = false

    while @attendees < @event.max_attendees
      movedAnyone = false

      waitlist = AttendanceRecord.find(:all, :conditions => {:event_id => @event, :waitlist => 1}, :order => "id ASC")
      if waitlist.size > 0
        if @event.drivers > 0
          drivers = AttendanceRecord.find(:all, :conditions => {:event_id => @event, :driver => 1, :waitlist => 1}, :order => "id ASC")
          if drivers.size > 0
            if @drivers < @event.drivers
              waitlistToAttendance(drivers[0], @event)
              movedAnyone = true
            else
              waitlist[0].driver = false
              waitlistToAttendance(waitlist[0], @event)
              movedAnyone = true
            end
          else
            if @attendees < @event.max_attendees and @drivers >= @event.drivers
              waitlistToAttendance(waitlist[0], @event)
              movedAnyone = true
            elsif @drivers < @event.drivers
              if @attendees - @drivers < @event.max_attendees - @event.drivers
                waitlistToAttendance(waitlist[0], @event)
                movedAnyone = true
              end
            end
          end
        else
          if @attendees < @event.max_attendees
            waitlistToAttendance(waitlist[0], @event)
              movedAnyone = true
          end
        end
      end

      break unless movedAnyone

      @attendees = AttendanceRecord.find(:all, :conditions => {:event_id => @event, :waitlist => 0}).size
      @drivers = AttendanceRecord.find(:all, :conditions => {:event_id => @event, :driver => 1, :waitlist => 0}).size

    end

    if @attendees >= @event.max_attendees
      @event.full = true
    else
      @event.full = false
    end
    @event.save

  end

  def self.waitlistToAttendance(waitlist, event)

    waitlist.waitlist = 0
    member = Member.find(waitlist.member_id)
    member.up_to_date = false
    member.save
    waitlist.save
    Emailer.deliver_signup(member.email, event)

  end

  def semester
    if (date.month > 6)
      "Fall #{date.year}"
    else
      "Spring #{date.year}"
    end
  end

end
